package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.entity.po.JobFavorite;
import com.lingnan.entity.po.JobPosition;
import com.lingnan.mapper.JobFavoriteMapper;
import com.lingnan.mapper.JobPositionMapper;
import com.lingnan.service.JobFavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class JobFavoriteServiceImpl extends ServiceImpl<JobFavoriteMapper, JobFavorite> implements JobFavoriteService {

    @Autowired
    private JobFavoriteMapper jobFavoriteMapper;

    @Autowired
    private JobPositionMapper jobPositionMapper;
    
    @Override
    public void addFavorite(Long seekerId, Long positionId) {
        // 检查是否已收藏
        if (isFavorited(seekerId, positionId)) {
            throw new RuntimeException("该职位已在收藏列表中");
        }
        
        // 检查职位是否存在且已发布
        LambdaQueryWrapper<JobPosition> positionQuery = new LambdaQueryWrapper<>();
        positionQuery.eq(JobPosition::getId, positionId)
                    .eq(JobPosition::getStatus, 1); // 只能收藏已发布的职位
        Long l = jobPositionMapper.selectCount(positionQuery);
        if (l == 0) {
            throw new RuntimeException("职位不存在或未发布");
        }
        
        JobFavorite favorite = new JobFavorite();
        favorite.setSeekerId(seekerId);
        favorite.setPositionId(positionId);
        
        save(favorite);
        log.info("求职者{}收藏职位{}成功", seekerId, positionId);
    }
    
    @Override
    public void removeFavorite(Long seekerId, Long positionId) {
        LambdaQueryWrapper<JobFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JobFavorite::getSeekerId, seekerId)
               .eq(JobFavorite::getPositionId, positionId);
        
        boolean removed = remove(wrapper);
        if (!removed) {
            throw new RuntimeException("收藏记录不存在");
        }
        
        log.info("求职者{}取消收藏职位{}成功", seekerId, positionId);
    }
    
    @Override
    public boolean isFavorited(Long seekerId, Long positionId) {
        return baseMapper.checkIfFavorited(seekerId, positionId) > 0;
    }
    
    @Override
    public List<Map<String, Object>> getFavoriteList(Long seekerId) {
        return baseMapper.selectFavoritePositionsWithDetails(seekerId);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchRemoveFavorites(Long seekerId, List<Long> positionIds) {
        if (CollectionUtils.isEmpty(Collections.singleton(positionIds))) {
            return;
        }
        
        LambdaQueryWrapper<JobFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JobFavorite::getSeekerId, seekerId)
               .in(JobFavorite::getPositionId, positionIds);
        
        remove(wrapper);
        log.info("求职者{}批量取消收藏{}个职位", seekerId, positionIds.size());
    }
}